package com.bw.mallshop.bind;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bw.mallshop.R;
import com.bw.mallshop.adapter.HomeBannerAdapter;
import com.bw.mallshop.adapter.HomeHotAdapter;
import com.bw.mallshop.adapter.HomeMenuAdapter;
import com.bw.mallshop.adapter.HomeTxtAdapter;
import com.bw.mallshop.adapter.HomeVpAdapter;
import com.bw.mallshop.entity.HomeBannerEntity;
import com.bw.mallshop.entity.HomeDedtailEntity;
import com.bw.mallshop.entity.HomeMenuEntity;
import com.bw.mallshop.widget.HomeGoodsDivider;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

public class HomeBind {

    @BindingAdapter({"itemClickListener","homeAdapter","bannerList",
            "textList","menuList","hotList","vpList","transformer"})
    public static void homeBind(RecyclerView recyclerView,
                                OnItemClickListener itemClickListener,
                                BaseQuickAdapter homeAdapter,
                                List<HomeBannerEntity> homeBannerEntities,
                                List<String> txtBanneres,
                                List<HomeMenuEntity>menues,
                                List<String>hotImges,
                                List<HomeBannerEntity>topEntities,
                                ViewPager.PageTransformer transformer){
        GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new HomeGoodsDivider(recyclerView.getContext()));
        homeAdapter.setOnItemClickListener(itemClickListener);
        //获取banner头布局
        View headView = LayoutInflater.from(recyclerView.getContext())
                .inflate(R.layout.view_home_banner,recyclerView,false);
        Banner home_view_banner = headView.findViewById(R.id.home_view_banner);
        HomeBannerAdapter bannerAdapter = new HomeBannerAdapter(homeBannerEntities);
        home_view_banner.setAdapter(bannerAdapter)
                .addBannerLifecycleObserver((LifecycleOwner) recyclerView.getContext())
                .setIndicator(new CircleIndicator(recyclerView.getContext()))
                .start();
        Banner home_text_banner = headView.findViewById(R.id.home_text_banner);
        HomeTxtAdapter txtAdapter = new HomeTxtAdapter(txtBanneres);
        home_text_banner.setAdapter(txtAdapter)
                .setOrientation(Banner.VERTICAL)
                .addBannerLifecycleObserver((LifecycleOwner) recyclerView.getContext())
                .start();
        RecyclerView home_menu_rv = headView.findViewById(R.id.home_menu_rv);
        GridLayoutManager menuManager = new GridLayoutManager(home_menu_rv.getContext(),4);
        menuManager.setOrientation(GridLayoutManager.VERTICAL);
        home_menu_rv.setLayoutManager(menuManager);
        HomeMenuAdapter menuAdapter = new HomeMenuAdapter();
        home_menu_rv.setAdapter(menuAdapter);
        menuAdapter.setNewInstance(menues);
        RecyclerView home_hot_rv = headView.findViewById(R.id.home_hot_rv);
        LinearLayoutManager hotManager = new LinearLayoutManager(home_hot_rv.getContext());
        hotManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        home_hot_rv.setLayoutManager(hotManager);
        HomeHotAdapter hotAdapter = new HomeHotAdapter();
        home_hot_rv.setAdapter(hotAdapter);
        hotAdapter.setNewInstance(hotImges);
        ViewPager home_vp = headView.findViewById(R.id.home_vp);
        home_vp.setOffscreenPageLimit(5);
        HomeVpAdapter vpAdapter = new HomeVpAdapter(topEntities);
        home_vp.setPageTransformer(true,transformer);
        home_vp.setAdapter(vpAdapter);
        homeAdapter.addHeaderView(headView);
        recyclerView.setAdapter(homeAdapter);
    }

    @BindingAdapter("refreshload")
    public static void smartRefreshBind(SmartRefreshLayout smartRefreshLayout,
                                        OnRefreshLoadMoreListener refreshload){
        ClassicsHeader header = new ClassicsHeader(smartRefreshLayout.getContext());
        ClassicsFooter foot = new ClassicsFooter(smartRefreshLayout.getContext());
        smartRefreshLayout.setRefreshHeader(header);
        smartRefreshLayout.setRefreshFooter(foot);
        smartRefreshLayout.setOnRefreshLoadMoreListener(refreshload);
    }

    @BindingAdapter({"detailentity","adapter"})
    public static void valuesBind(RecyclerView recyclerView, HomeDedtailEntity entity,BaseQuickAdapter adapter){
        //由于当前HomeDedtailEntity->商品详情数据使用Databinding下被观察者包裹
        //在viewmodel中只要使用对应被观察者set添加数据一次valueBind方法就会被调用一次
        //BaseQuickAdapter只需要向recyclerview添加一次
        //区分是否添加完成适配器
        ImageView value_head_img = null;
        TextView value_head_title = null;
        TextView value_head_dec = null;
        TextView mSkuLabelTv = null;
        RecyclerView.Adapter recyclerAdapter = recyclerView.getAdapter();//获取recyclerview关联的适配器
        if (recyclerAdapter == null){
            GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),2);
            manager.setOrientation(GridLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);
            recyclerView.addItemDecoration(new HomeGoodsDivider(recyclerView.getContext()));
            View headView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.view_homevalue,null);
            value_head_img = headView.findViewById(R.id.value_head_img);
            value_head_title = headView.findViewById(R.id.value_head_title);
            value_head_dec = headView.findViewById(R.id.value_head_dec);
            mSkuLabelTv = headView.findViewById(R.id.mSkuLabelTv);
            adapter.addHeaderView(headView);
            recyclerView.setAdapter(adapter);
            if (entity!=null){
                //添加头布局数据
                Glide.with(recyclerView.getContext()).load(entity.data.getGoods_detail_one())
                        .centerCrop().into(value_head_img);
                value_head_title.setText(entity.data.getGoods_default_price());
                value_head_dec.setText(entity.data.getGoods_desc());
                mSkuLabelTv.setText(entity.data.getGoodsSku().get(0).getGoods_sku_title());
            }
        }else {
            if (entity!=null){
                //添加头布局数据
                BaseQuickAdapter baseQuickAdapter = (BaseQuickAdapter) recyclerAdapter;
                View headView = baseQuickAdapter.getHeaderLayout().getChildAt(0);
                value_head_img = headView.findViewById(R.id.value_head_img);
                value_head_title = headView.findViewById(R.id.value_head_title);
                value_head_dec = headView.findViewById(R.id.value_head_dec);
                mSkuLabelTv = headView.findViewById(R.id.mSkuLabelTv);
                Glide.with(recyclerView.getContext()).load(entity.data.getGoods_detail_one())
                        .centerCrop().into(value_head_img);
                value_head_title.setText(entity.data.getGoods_default_price());
                value_head_dec.setText(entity.data.getGoods_desc());
                mSkuLabelTv.setText(entity.data.getGoodsSku().get(0).getGoods_sku_title());
            }
        }
    }

}
